<?php
/* 
* @Author: Administrator
* @Date:   2014-06-17 22:21:26
* @Last Modified by:   Administrator
* @Last Modified time: 2014-07-22 21:07:41
*/


class LoginControl extends Control{
	public function index()
	{
		if(isset($_SESSION['username']) && isset($_SESSION['uid'])){
			go(U('Index/index'));
		}
		if(IS_POST){
			$model = K('User');

			if (!$model->create()) $this->error($model->error);
			// p($_POST);
			$userInfo = $model->validate_login();
			if(!$userInfo) $this->error($model->error);
			//存入session
			session('username',$userInfo['username']);
			session('uid',$userInfo['uid']);
			session('rid',$userInfo['rid']);
			session('usermail',$userInfo['usermail']);
			//登录信息
			session('loginip',$userInfo['loginip']);
			session('logintime',$userInfo['logintime']);
			//修改登录信息
			$model->update_login();
			//判断是否自动登录
			if(Q('post.auto',NULL,'intval')){
				setcookie(session_name(),session_id(),time()+3600 * 24 * 7, '/');
			}
			$this->success('登录成功');
		}
		$this->display();
	}

	public function out()
	{
		session(NULL);
		$this->success('退出成功',U('Index/Index/index'));
	}
}
?>
